<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$merch=pdo_get('jy_merch_user',array('id'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']));
	$return['plugin_icon']=$merch['logo'];
	$return['item']=array('merchid'=>$_SESSION['onemerchid']);
	$return['item']['excelurl']=SITEROOT.'/addons/jy_weishopniu/resource/test.CSV';
	$return['item']['excelurl']='请直接由本模块导出数据zip包，解压后，按csv名称选择导入！';
	$return['htmls']=array (
		  'type' => 'edit',
		  'post' => 'post',
		  'edit_tab' => '商品导入',
		  'edits' => array (
		    array (
		      'title' => '商品导入',
		      'list' => array (
				array ('type' => 'tip','title' => '<red>[温馨提示]：在导入过程中，请不要进行任何操作!</br>功能介绍：可将本模块导出的CSV文件快速上传至商城,节约您的大量时间!</br>使用方法：</br> 1. 将您获取到的CSV文件,并且导入</br>2. 如果共用图片，本步骤可以忽略。将配套的图片文件包压缩为Zip格式压缩包并且导入(图片需在压缩包根目录下，正常情况下为了节约服务器资源，可以共用图片)</br>3.最后，确认上传即可</red>'),
				array ('name' => 'excelurl','type' => 'copy','title' => 'CSV示例文件下载'),
				//array ('name' => 'zipurl','type' => 'down','title' => 'Zip示例文件下载'),
				array ('name' => 'type','type' => 'radio','title' => '选择导入数据类型','options'=>'types','help'=>'务必选择准确，否则导入数据异常！'),
				array ('name' => 'excel','type' => 'upfile','title' => '上传CSV'),
				//array ('name' => 'zip','type' => 'upfile','title' => '上传ZIP'),
			 ),
		    ),
		  ),
	);
	$return['types']=array(0=>'商品',1=>'分类',2=>'门店',3=>'总规格',4=>'规格名称',5=>'规格序');
	unset($return['types'][2]);
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$merchid=$post['merchid'];
	if(!$post['excel']){
		exi('请先上传CSV文件！','error');
	}
	//$zip=cfc('files')->local($post['zip']);
	$excel=cfc('files')->local($post['excel']);
	$excel=cfc('tools')->importExcel($excel);
	cfc('files')->source_del($post['excel']);
	$res=$this->model('model')->get_csvitems($excel);
	if($post['type']==0){		
		$this->model('model')->save_csv_goods($res,$merchid);
	}elseif($post['type']==1){
		$this->model('model')->save_csv_group($res,$merchid);
	}elseif($post['type']==2){
		$this->model('model')->save_csv_store($res,$merchid);
	}elseif($post['type']==3){
		$this->model('model')->save_csv_option($res,$merchid);
	}elseif($post['type']==4){
		$this->model('model')->save_csv_spec($res,$merchid);
	}elseif($post['type']==5){
		$this->model('model')->save_csv_spec_item($res,$merchid);
	}
	exi('导入文件成功！');
}
if($_RQ['op']=='updata'){
	$merchid=$_SESSION['onemerchid'];
	$num = intval($_RQ['num'])?:0;
	$items=cache_read('goods_rucsv'.$_SESSION['uid'],'jy_weishopniu');
	$items=json_decode($items,true);
	$totalnum = count($items);
	$this->model('model')->save_csv_goods($items[$num],$merchid);
	if ($totalnum <= $num + 1) {
			cache_delete('goods_rucsv'.$_SESSION['uid'],'jy_weishopniu');
			exi('全部导入成功！','','jy_weishopniu/goods/');
	}else{
			$num++;
			exi('已成功导入第【'.$num.'】个商品！！','','jy_weishopniu/goods_ru/updata/num='.$num);
	}
}
?>